/*
 * Copyright 2025 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import org.ukui.quick.items 1.0 as UkuiItems
import org.ukui.quick.platform 1.0

UkuiItems.StyleBackground {
    id: searchContainer

    property bool isCentered: true
    property alias searchInputFocus: searchInput.focus
    property alias searchInputFocusActive: searchInput.activeFocus
    property alias text: searchInput.text

    signal searchTextChanged(string text)

    radius: 6
    useStyleTransparency: false

    RowLayout {
        id: layout

        spacing: 8
        states: [
            State {
                name: "leftAligned"
                when: !isCentered

                AnchorChanges {
                    target: layout
                    anchors.horizontalCenter: undefined
                    anchors.left: parent.left
                }

                PropertyChanges {
                    target: layout
                    anchors.leftMargin: 20
                }

            }
        ]

        anchors {
            horizontalCenter: parent.horizontalCenter
            verticalCenter: parent.verticalCenter
        }

        Rectangle {
            id: searchIcon

            width: 16
            height: 16
            color: "transparent"

            UkuiItems.Icon {
                anchors.fill: parent
                source: "edit-find-symbolic"
                mode: UkuiItems.Icon.AutoHighlight
            }

        }

        UkuiItems.StyleText {
            id: placeholderText

            text: qsTr("Search")
        }

    }

    TextInput {
        id: searchInput

        verticalAlignment: Text.AlignVCenter
        font.pointSize: 14
        clip: true
        onTextChanged: {
            onTextChanged:
            {
                searchTextChanged(text);
            };
        }
        onActiveFocusChanged: {
            state = focus ? "active" : (text === "" ? "inactive" : "active");
            if (focus) {
                placeholderText.text = "";
                isCentered = false;
            } else {
                placeholderText.text = qsTr("Search");
                isCentered = true;
                text = "";
            }
        }

        anchors {
            fill: parent
            leftMargin: 40
            rightMargin: 20
        }

    }

    transitions: Transition {
        AnchorAnimation {
            duration: 200
            easing.type: Easing.OutQuad
        }

    }

}
